package DataFile;

import java.nio.ByteBuffer;
import java.util.Formatter;

import ClassData.ExceptionHandler;

/**
 * 
 * @note change by Dominik Ollesz
 * @see ClassData.ExceptionHandler
 */
public class Structure_ExceptionHandler extends Structure_Template {

	ExceptionHandler myExceptionHandler;

	public Structure_ExceptionHandler(ExceptionHandler myExceptionHandler) {
		this.myExceptionHandler = myExceptionHandler;
	}
	
	@Override
	protected int setBaseAddress(int newBaseAdress) {
		baseAdress = newBaseAdress;
		return(baseAdress+getMemorySize());
	}
	
	@Override
	protected int getMemorySize() {
		int size = 0;
		size  = SHORT_WIDTH; // start_pc
		size += SHORT_WIDTH; // end_pc
		size += SHORT_WIDTH; // handler_pc
		size += SHORT_WIDTH; // catch_pc
		return(size);
	}

	@Override
	protected String getMemoryMapAsString() {
		String returnString = "";
		returnString+=" start_pc - " + myExceptionHandler.getStartPC();
		returnString+=" end_pc - " + myExceptionHandler.getEndPC();
		returnString+=" handler_pc - " + myExceptionHandler.getHandlerPC();
		returnString+=" catch_type - " + myExceptionHandler.getCatchType();
		returnString+="\n";
		return returnString;
	}
	
	protected String getMemoryMapAsString(Structure_CP_Table cpTable) {
		String returnString = "";
		returnString+=" start_pc - "    + new Formatter().format("%3d",myExceptionHandler.getStartPC());
		returnString+=" end_pc - "      + new Formatter().format("%3d",myExceptionHandler.getEndPC());
		returnString+=" handler_pc - "  + new Formatter().format("%3d",myExceptionHandler.getHandlerPC());
		returnString+=" catch_type - #" + myExceptionHandler.getCatchType();
		returnString+=" -> " + cpTable.getClassName(myExceptionHandler.getCatchType());
		returnString+="\n";
		return returnString;
	}
	
	public void writeData(ByteBuffer fileData) {
		fileData.position(baseAdress-DataFileCreator.BASE_MEMORY_ADRESS);
		fileData.putShort(myExceptionHandler.getStartPC());     // 2 Bytes
		fileData.putShort(myExceptionHandler.getEndPC());       // 2 Bytes
		fileData.putShort(myExceptionHandler.getHandlerPC());   // 2 Bytes
		fileData.putShort(myExceptionHandler.getCatchType());   // 2 Bytes
	}
	
}
